<?php

namespace App\Http\Controllers;

use App\Http\Resources\BaseResource;
use App\Http\Resources\ErrorResource;
use App\Repositories\Contracts\TcCommonPolicyInterface;
use Illuminate\Http\Request;
use MakeRep\Controllers\BaseController;
use MakeRep\Resources\BaseCollection;
use MakeRep\Services\ParamService;

class TcCommonPolicyController extends BaseController
{
    public function __construct(TcCommonPolicyInterface $interface)
    {
        parent::__construct($interface);
    }

    /**
     * 上传政策
     */
    public function ajaxAdd(Request $request)
    {
        $res = $this->interface->create($request->all());
        if ($res === true) {

            return new BaseResource([]);
        }

        return new ErrorResource(['errmsg'=>$res]);
    }

    /**
     * 列表
     * @param Request $request
     * @return BaseCollection
     */
    public function ajaxLists(Request $request){

        $pageSize = $request->page_size ? : 10;
        $orderBy = $request->order_by ? : 'desc';
        $where = ParamService::createCondition($request,isset($this->interface->fillable) ? $this->interface->fillable : []);
        $res = $this->interface->pageLists($where,'*',$pageSize,'id',$orderBy);

        foreach ($res as $key => &$value) {
            $policyId = $value['policy_id'];
            if (!empty($policyId)) {
                $tcPolicyInterface = resolve('App\Repositories\Ota\Tc\Policy\Contracts\TcCommonPolicyInterface');
                $tcPolicyInfo = $tcPolicyInterface->getPolicyInfoById($policyId);
                if ($tcPolicyInfo) {
                    if ($tcPolicyInfo['data']) {
                        $policy = $tcPolicyInfo['data'][0];
                        if ($policy) {
                            $value['seat'] = $value['seat'] . '/' . $policy['everydayInventory'];
                        }
                    }
                }
            } else {
                $value['seat'] = $value['seat'] . '/' . $value['seat'];
            }
        }
        return new BaseCollection($res);
    }

    /**
     * 转到详情页面
     */
    public function update(Request $request){

        $where = $request->id ? ['id'=>$request->id] : [];
        $res = $this->interface->index($where);
        if ($res) {
            $policyId = $res['policy_id'];
            $tcPolicyInterface = resolve('App\Repositories\Ota\Tc\Policy\Contracts\TcCommonPolicyInterface');
            $tcPolicyInfo = $tcPolicyInterface->getPolicyInfoById($policyId);
            if ($tcPolicyInfo) {
                if($tcPolicyInfo['data']) {
                    $policy = $tcPolicyInfo['data'][0];
                    if ($policy) {
                        $policy['id'] = $res['id'];
                        $policyInfo = ['policy' => $policy];
                        return view('tccommonpolicy/update', $policyInfo);
                    }
                }
            }
        }
        abort(404);
    }


    /**
     * 更新政策
     */
    public function ajaxUpdate(Request $request){
        $res = $this->interface->updatePolicy($request->all());
        if ($res === true) {

            return new BaseResource([]);
        }
        return new ErrorResource(['errmsg'=>$res]);
    }

    /**
     * 删除政策
     */
    public function ajaxDelete(Request $request){
        $res = $this->interface->delPolicy($request->all());
        if ($res === true) {

            return new BaseResource([]);
        }
        return new ErrorResource(['errmsg'=>$res]);
    }

}